package com.kzd.gtgame.jetpack.manager

import com.kzd.gtgame.app.*
import com.kzd.gtgame.base.BaseManager
import com.kzd.gtgame.entity.*
import com.module.lemlin.http.CustomException
import com.module.lemlin.http.HttpResponseData
import com.module.lemlin.http.HttpResponseDataE
import com.module.lemlin.http.HttpResponseDataT
import rxhttp.RxHttp
import rxhttp.await

class MoneyRepository : BaseManager() {

    /**
     * 签到数据
     * */
    suspend fun moneySign(): HttpResponseDataT<MoneySign> =
        RxHttp.postForm(PERSONAL_GET_SIGN_IN_CONFIG)
            .await()

    /**
     * 签到
     * */
    suspend fun moneySignSubmit(): HttpResponseData =
        RxHttp.postForm(PERSONAL_SIGN_IN)
            .await()

    /**
     * 新手任务列表
     * */
    suspend fun moneyTaskNew(): HttpResponseDataT<MoneyTaskNew> =
        RxHttp.postForm(USER_GET_NEWBIE_TASK)
            .await()

    /**
     * 新手任务领取
     * */
    suspend fun moneyTaskNewGet(type: String): HttpResponseData =
        RxHttp.postForm(USER_XIN_SHOU_RECEIVE)
            .add("type", type)
            .await()

    /**
     * 分享app成功
     * */
    suspend fun moneyTaskAddShare(): HttpResponseData =
        RxHttp.postForm(USER_ADD_SHARE)
            .await()

    /**
     * 日常任务列表
     * */
    suspend fun moneyTaskList(): HttpResponseDataE<MoneyTaskList> =
        RxHttp.postForm(USER_GET_MAKE_MONEY)
            .await()

    /**
     * 任务列表
     * */
    suspend fun moneyTaskDay(): HttpResponseDataE<MoneyTask> =
        RxHttp.postForm(USER_GET_TODAY_TASK)
            .await()

    /**
     * 任务列表领取
     * */
    suspend fun moneyTaskDayGet(type: String): HttpResponseData =
        RxHttp.postForm(USER_TODAY_RECEIVE)
            .add("type", type)
            .await()

    /**
     * 试玩任务
     * */
    suspend fun moneyTaskPlay(
        page: String, limit: String
    ): HttpResponseDataE<MoneyTaskPlay> = RxHttp.postForm(PERSONAL_RENWU_LIST)
        .add("p", page)
        .add("limit", limit)
        .await()

    /**
     * 试玩报名、领取
     * */
    suspend fun moneyTaskPlayGet(id: String): HttpResponseData =
        RxHttp.postForm(PERSONAL_RENWU_CHANGE)
            .add("id", id)
            .await()

    /**
     * 积分商城
     * */
    suspend fun moneyTaskMall(): HttpResponseDataE<MoneyTaskMall> =
        RxHttp.postForm(NEW_PRODUCT_MALL)
            .await()

    /**
     * 积分商城详情
     * */
    suspend fun moneyTaskMallDetails(id: String): HttpResponseDataT<MallDetails> =
        RxHttp.postForm(APP_MALL_INFO)
            .add("id", id)
            .await()

    /**
     * 积分商城查询
     * */
    suspend fun moneyTaskMallQuery(id: String): HttpResponseData =
        RxHttp.postForm(NEW_PRODUCT_MALL_QUERY)
            .add("id", id)
            .await()

    /**
     * 积分商城兑换
     * */
    suspend fun moneyTaskMallSubmit(
        sign: String, time: String, goodId: String, addressId: String, num: String
    ): HttpResponseDataT<String> = RxHttp.postForm(APP_MALL_BUY)
        .add("sign", sign)
        .add("t", time)
        .add("id", goodId)
        .add("address_id", addressId)
        .add("num", num)
        .await()

    /**
     * 大佬须知-共享列表
     * */
    suspend fun moneyBossShared(): HttpResponseDataE<MoneyBossShared> =
        RxHttp.postForm(USER_SHARE_ORDER_LIST)
            .await()

    /**
     * 大佬须知-邀请列表
     * */
    suspend fun moneyBossInvite(): HttpResponseDataE<MoneyBossInvite> =
        RxHttp.postForm(USER_INVITE_NOTICE)
            .await()

    /**
     * 我的体验金
     * */
    suspend fun moneyExperience(
        page: String, limit: String
    ): HttpResponseDataE<MoneyExperience> = RxHttp.postForm(USER_GET_EXPERIENCE_COIN)
        .add("p", page)
        .add("limit", limit)
        .await()

    /**
     * 合并-（新手任务列表，日常任务列表）
     * */
    suspend fun moneyData(): HttpResponseDataT<MoneyData> {
        val responseSign = moneySign()
        if (responseSign.code != 1) throw CustomException(
            responseSign.code.toString(), responseSign.message
        )
        val responseNew = moneyTaskNew()
        if (responseNew.code != 1) throw CustomException(
            responseNew.code.toString(), responseNew.message
        )
        val responseList = moneyTaskList()
        if (responseList.code != 1) throw CustomException(
            responseList.code.toString(), responseList.message
        )
        val moneyData = MoneyData(
            sign = responseSign.data, taskNew = responseNew.data, taskDay = responseList.data
        )
        return HttpResponseDataT(moneyData).apply {
            code = responseList.code
            message = responseList.message
        }
    }
}